<?php

namespace app\api\controller\user;

use think\Controller;
use think\Request;

class Address extends Controller {

    public function __construct()
    {
        parent::__construct();
        $this->AddressModel = model('User.Address');
    }
    
    // 收货地址
    public function index() {
        $userid = $this->request->param('userid');
        $AddressList = $this->AddressModel->with(['provinces', 'citys', 'districts'])
        ->where(['userid' => $userid])->select();
        // halt($AddressList);
        // if (empty($AddressList)) {
        //     $this->error('暂无收货地址');
        // }
        $this->success('查询成功', null, $AddressList);
        
    }
    // 选择默认地址
    public function select() {
        $id = $this->request->param('id');
        $userid = $this->request->param('userid');
        $user = $this->AddressModel->where(['userid' => $userid])->find();
        if (!$user) {
            $this->error('您暂时无权修改该地址');
        }
        $address = $this->AddressModel->find($id);
        if (!$address) {
            $this->error('收货地址不存在');
        }
        $this->AddressModel->where(['userid' => $userid])->update(['status' => 0]);

        $data = [
            'id' => $id,
            'status' => 1
        ];
        $res = $this->AddressModel->isUpdate(true)->save($data);
        if ($res == false) {
        }
        $this->success('修改默认地址成功');
    }

    // 添加收货
    public function add() {
        $params = $this->request->param();
        $data = [
            'consignee' => $params['consignee'],
            'mobile' => $params['mobile'],
            'address' => $params['address'],
            'userid' => $params['userid'],
        ];

        // 设置默认值
        if ($params['status'] == 1) {
            $this->AddressModel->where(['userid' => $params['userid']])->update(['status' => 0]);
            $data['status'] = $params['status'];
        }
        if (!empty($params['code'])) {
            $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');
            if (!$parentpath) {
                $this->error('所选地区不存在');
            }
            [$province, $city, $district] = explode(',', $parentpath);
            if ($province) {
                $data['province'] = $province;
            }
            if ($city) {
                $data['city'] = $city;
            }
            if ($district) {
                $data['district'] = $district;
            }
        }
        $result = $this->AddressModel->validate('common/User/Address')->save($data);
        if ($result == false) {
            $this->error($this->AddressModel->getError());
        }
        $this->success('新增收货地址成功', '/user/address/index');
    }

    // 获取一条收货地址信息
    public function info() {
        $id = $this->request->param('id');
        // halt($params);
        $addinfo = $this->AddressModel->with(['provinces','citys','districts'])->find($id);
        if (!$addinfo) {
            $this->error('获取信息失败');
        }
        // halt($addinfo);
        $this->success('获取信息成功', null, $addinfo);
    }

    // 更新收货地址
    public function edit() {
        $params = $this->request->param();
        // halt($params);
        $data = [
            'id' => $params['id'],
            'consignee' => $params['consignee'],
            'mobile' => $params['mobile'],
            'address' => $params['address'],
            'userid' => $params['userid'],
        ];

        if ($params['status'] == 1) {
            $this->AddressModel->where(['userid' => $params['userid']])->update(['status' => 0]);
            $data['status'] = $params['status'];

        }
        if (!empty($params['code'])) {
            $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');
            if (!$parentpath) {
                $this->error('所选地区不存在');
            }
            [$province, $city, $district] = explode(',', $parentpath);
            if ($province) {
                $data['province'] = $province;
            }
            if ($city) {
                $data['city'] = $city;
            }
            if ($district) {
                $data['district'] = $district;
            }
        }
        $info = $this->AddressModel->isUpdate(true)->save($data);
        if ($info == false) {
            $this->error('更新收货地址失败');
        }
        $this->success('更新收货地址成功', '/user/address/index');
    }

    // 删除收货地址
    public function del () {
        $id = $this->request->param('id');
        // halt($id);
        $addr =  $this->AddressModel->find($id);
        if (empty($addr)) {
            $this->error('无法删除！');
        }
        $res =  $this->AddressModel->where('id', $id)->delete();
        if (!$res) {
            $this->error('删除失败');
        }
        $this->success('删除成功', '/user/address/index');
    }



}
